package com.wuhongyu.service.impl;

import com.wuhongyu.base.BaseServiceImpl;
import com.wuhongyu.common.MyCommon;
import com.wuhongyu.entity.Server;
import com.wuhongyu.repository.ServerDao;
import com.wuhongyu.repository.UserDao;
import com.wuhongyu.service.ServerService;
import com.wuhongyu.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("serverService")
public class ServerServiceImpl extends BaseServiceImpl<Server> implements ServerService {

    @Autowired
    private UserDao userDao;

    private ServerDao serverDao;
    @Resource
    public void setDao(ServerDao serverDao){
        super.setBaseDao(serverDao);
        this.serverDao = serverDao;
    }
    @Override
    public Page<Server> findAll(Server server, Integer number) {
        Specification<Server> spec = (root,query,cb) ->{
            List<Predicate> list = new ArrayList<>();
            if(!StringUtils.isEmpty(server.getName()))
                list.add(cb.and(cb.like(root.get("name"),"%"+server.getName()+"%")));
            if(!StringUtils.isEmpty(server.getOutline()))
                list.add(cb.and(cb.like(root.get("outline"),"%"+server.getOutline()+"%")));
            if(!StringUtils.isEmpty(server.getType()))
                list.add(cb.and(cb.equal(root.get("type"),server.getType())));
            if(server.getBegintime()!=null)
                list.add(cb.and(cb.greaterThan(root.get("createtime"),server.getBegintime())));
            if(server.getEndtime()!=null)
                list.add(cb.and(cb.lessThan(root.get("createtime"),server.getEndtime())));
            if(!StringUtils.isEmpty(server.getStatus()))
                list.add(cb.and(cb.equal(root.get("status"),server.getStatus())));
            return cb.and(list.toArray(new Predicate[list.size()]));
        };
        return serverDao.findAll(spec, new PageRequest(MyUtil.getCurrentPage(number), MyCommon.PAGESIZE));
    }

    @Override
    public void save(Server server) {
        //状态为空则为添加
        if(server.getStatus()==null){
            server.setStatus(1);
            serverDao.saveAndFlush(server);
            return ;
        }else if(server.getStatus()==1){
            server.setStatus(2);
            server.setAppointtime(new Date());
            serverDao.saveAndFlush(server);
            return ;
        }
        //不为空则为修改
        Server server1 = serverDao.getOne(server.getId());
        //状态等于2则是服务处理
        if(server.getStatus()==2){
            if(server.getHandle()!=null){
                server1.setHandle(server.getHandle());
            }
            if(server.getHandleUser()!=null){
                server1.setHandleUser(userDao.getOne(server.getHandleUser().getId()));
            }
            if(server.getHandletime()!=null){
                server1.setHandletime(server.getHandletime());
            }
        }
        //不等于2必然等于3 则是反馈
        if(server.getResult()!=null){
            server1.setResult(server.getResult());
        }
        if(server.getSatisfaction()!=null){
            server1.setSatisfaction(server.getSatisfaction());
        }
        server1.setStatus(server1.getStatus()+1);
        serverDao.saveAndFlush(server1);
    }

}